﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;

namespace wnet.admin
{
    public class SensorBusiness : CatchingProcessor
    {

        #region Fileds
        /// <summary>
        /// 探测器字典
        /// </summary>
        Dictionary<int, int> SensorTypes = new Dictionary<int, int>() { };

        private int PackageLength = 83;
        /// <summary>
        /// 跳过记录显示点
        /// </summary>
        private int mSkipNum = 0;

        public ObservableCollection<ObservableCollection<Point>> localData = new ObservableCollection<ObservableCollection<Point>>();

        /// <summary>
        /// 数据坐标点 第一组数据和第二组数据的同步 数据源为第一组数据
        /// </summary>
        private Queue<int> mYPoint = new Queue<int>();

        private int position = 0;

        private Project Project = new Project();
        #endregion

        #region Property

        #endregion

        #region Construct
        public SensorBusiness(string procesorName, int mThreadCount) : base(procesorName, mThreadCount)
        {
            Init();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="data"></param>
        public void InitData(ObservableCollection<ObservableCollection<Point>> data)
        {
            this.localData = data;
        }
        public void Init()
        {
            ConstDefine.AllData.Clear();
            if (ConstDefine.AllData.Count == 0)
            {
                ConstDefine.AllData.Clear();
                for (int i = 0; i < ConstDefine.Project.SenSorNum * ConstDefine.Project.BoardNum; i++)
                {
                    ConstDefine.AllData.Add(new List<Point>());
                }
            }
        }
        #endregion

        #region Private Method
        #endregion

        #region Public Method
        /// <summary>
        /// 消费队列数据                 //fileRecord.WriteFile(allRevByte);
        /// </summary>
        /// <param name="obj"></param>
        public override void Process(object obj)
        {
            byte[] allByte = obj as byte[];
            try
            {
                int mDetectorIndex = 0;
                int indexEB = 0;
                for (int bNum = 0; bNum < allByte.Length; bNum++)
                {
                    if (bNum + PackageLength <= allByte.Length)
                    {
                        if ((allByte[bNum] == 0xEB && allByte[bNum + 1] == 0x90) && (allByte[bNum + PackageLength - 2] == 0x0D && allByte[bNum + PackageLength - 1] == 0x0A))
                        {
                            if (SensorTypes.Count != 2 && !SensorTypes.ContainsKey(allByte[bNum + 3]))
                            {
                                SensorTypes.Add(allByte[bNum + 3], SensorTypes.Count);
                            }
                            SensorTypes.TryGetValue(allByte[bNum + 3], out mDetectorIndex);
                            DealData(allByte.Skip(bNum).Take(PackageLength).ToArray(), mDetectorIndex);
                            bNum += this.PackageLength - 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void DealData(byte[] allByte, int startSenSorIndex)
        {
            try
            {
                if (this.Project.SkipNum != 0)
                {
                    if (mSkipNum % this.Project.SkipNum != 0)
                    {
                        mSkipNum++;
                        return;
                    }
                    else
                    {
                        mSkipNum = 1;
                    }
                }

                byte[] data = allByte;

                byte[] positionByte = new byte[] { data[11], data[10], data[9], data[8] };
                int position = BitConverter.ToInt32(positionByte, 0);
                position = Math.Abs(position);
                dynamic value = 0;
                int index = 0;

                for (int j = 12; j <= 12 + (ConstDefine.Project.SenSorNum - 1) * 2; j += 2)
                {
                    value = BitConverter.ToUInt16(new byte[] { data[j + 1], data[j] }, 0);
                    Point point = new Point();
                    point.X = (int)(position);
                    point.Y = (int)(value);
                    //point.Y += (index + startSenSorIndex * ConstDefine.Project.SenSorNum) * 1000;
                    int indexData = index + startSenSorIndex * ConstDefine.Project.SenSorNum;
                    int mPointNum = 500;
                    if (ConstDefine.mIsEnvironmentRunning)
                    {
                        for (int i = 0; i < 64; i++)
                        {
                            //point.X += i * 10;
                            lock (ConstDefine.AllData[i])
                            {
                                if (ConstDefine.AllData[i].Count > mPointNum)
                                {
                                    int removeLength = ConstDefine.AllData[i].Count - mPointNum;
                                    ConstDefine.AllData[i].RemoveRange(0, removeLength);
                                }
                            }
                        }
                        lock (ConstDefine.AllData[index + startSenSorIndex * ConstDefine.Project.SenSorNum])
                        {
                            ConstDefine.AllData[index + startSenSorIndex * ConstDefine.Project.SenSorNum].Add(point);
                        }
                    }

                    index++;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        #endregion

    }
}